from typing import List
from pydantic import BaseModel

from services.server_integration.schemas.link import Link
from services.server_integration.schemas.channel import Channel
from services.automatic_control.schemas.configs import OutputPowerLinkConfig


class Rtr(BaseModel):
    central_frequency: Link[float] = Link[float](ul=7_020_000_000, dl=6_680_000_000)
    output_power_link: OutputPowerLinkConfig = OutputPowerLinkConfig()


class RtrCommon(BaseModel):
    enabled: Link[bool] = Link[bool](ul=False, dl=False)
    rt_rxs_enabled: List[bool] = [False, False]
    optic_enabled: List[bool] = [False, False]

    bandwidth: Link[float] = Link[float](ul=5000000, dl=5000000)  # ширина виртуального сервисного канкала
    frequency_offset: Link[float] = Link[float](ul=2500000, dl=2500000)


class RelayChannel(Channel):
    rtrs: List[Rtr] = [Rtr(), Rtr()]
    rtr_common: RtrCommon = RtrCommon()

    def set_rm_enable(self, index: int, state: bool):
        self.rtr_common.rt_rxs_enabled[index] = state

    def set_optic_enable(self, index: int, state: bool):
        self.rtr_common.optic_enabled[index] = state

    def set_rm_central_frequency(self, index: int, ul: float = None, dl: float = None):
        if ul is None:
            ul = self.rtrs[index].central_frequency.ul
        if dl is None:
            dl = self.rtrs[index].central_frequency.dl

        self.rtrs[index].central_frequency.ul = ul
        self.rtrs[index].central_frequency.dl = dl

    def set_rm_input_gain(self, index: int, gain: float):
        self.rtrs[index].output_power_link.input_gain = gain

    def set_rm_output_gain(self, index: int, gain: float):
        self.rtrs[index].output_power_link.output_gain = gain

    def set_enabled(self, ul: bool = None, dl: bool = None):
        if ul is None:
            ul = self.rtr_common.enabled.ul
        if dl is None:
            dl = self.rtr_common.enabled.dl

        self.rtr_common.enabled.ul = ul
        self.rtr_common.enabled.dl = dl

    def set_bandwidth(self, ul: float = None, dl: float = None):
        if ul is None:
            ul = self.rtr_common.bandwidth.ul
        if dl is None:
            dl = self.rtr_common.bandwidth.dl

        self.rtr_common.bandwidth.ul = ul
        self.rtr_common.bandwidth.dl = dl

    def set_frequency_offset(self, ul: float = None, dl: float = None):
        if ul is None:
            ul = self.rtr_common.frequency_offset.ul
        if dl is None:
            dl = self.rtr_common.frequency_offset.dl

        self.rtr_common.frequency_offset.ul = ul
        self.rtr_common.frequency_offset.dl = dl
